package com.settlement.system.service.impl;

import com.settlement.system.model.entity.SysOmsOrderLink;
import com.settlement.system.mapper.SysOmsOrderLinkMapper;
import com.settlement.system.service.SysOmsOrderLinkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.model.query.SysOmsOrderLinkQuery;
import com.settlement.system.model.form.SysOmsOrderLinkForm;
import cn.hutool.core.bean.BeanUtil;
import com.settlement.system.model.vo.SysOmsOrderLinkImportVo;

/**
 * <p>
 * OMS平台订单对应关系 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2024-05-06
 */
@Service
public class SysOmsOrderLinkServiceImpl extends ServiceImpl<SysOmsOrderLinkMapper, SysOmsOrderLink> implements SysOmsOrderLinkService {

    @Override
    public Page<SysOmsOrderLink> getPage(SysOmsOrderLinkQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysOmsOrderLink> page = new Page<>(pageNum, pageSize);
        return this.baseMapper.getPage(page, queryParams);
    }

     @Override
     public boolean saveSysOmsOrderLink(SysOmsOrderLinkForm sysOmsOrderLinkForm) {
         SysOmsOrderLink sysOmsOrderLink = new SysOmsOrderLink();
         BeanUtil.copyProperties(sysOmsOrderLinkForm, sysOmsOrderLink);
         return this.saveOrUpdate(sysOmsOrderLink);
     }

     @Override
     public SysOmsOrderLinkForm getSysOmsOrderLinkFormData(Long id) {
         SysOmsOrderLink sysOmsOrderLink = this.getById(id);
         SysOmsOrderLinkForm sysOmsOrderLinkForm = new SysOmsOrderLinkForm();
         BeanUtil.copyProperties(sysOmsOrderLink, sysOmsOrderLinkForm);
         return sysOmsOrderLinkForm;
     }

     @Override
     public boolean deleteSysOmsOrderLink(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
     }

    @Override
    public void importData(List<SysOmsOrderLinkImportVo> voList)  {
        List<SysOmsOrderLink> sysOmsOrderLinkList = new ArrayList<>();
        for (SysOmsOrderLinkImportVo importVo : voList) {
            SysOmsOrderLink sysOmsOrderLink = new SysOmsOrderLink();
            BeanUtil.copyProperties(importVo, sysOmsOrderLink);
            sysOmsOrderLinkList.add(sysOmsOrderLink);
        }
        this.saveOrUpdateBatch(sysOmsOrderLinkList);
    }

    @Override
    public List<SysOmsOrderLinkImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysOmsOrderLink> sysOmsOrderLinkList = this.getBaseMapper().selectBatchIds(ids);
        List<SysOmsOrderLinkImportVo> voList = new ArrayList<>();
        for (SysOmsOrderLink sysOmsOrderLink : sysOmsOrderLinkList) {
            SysOmsOrderLinkImportVo vo = new SysOmsOrderLinkImportVo();
            BeanUtil.copyProperties(sysOmsOrderLink, vo);
            voList.add(vo);
        }
        return voList;
    }
}
